import { Node, UITransform, v3, Vec3 } from "cc";
import GameConfig from "../constant/GameConfig";

export default class UIUtils {
  /**
   * 将node 裁剪到另一个node下
   * @param node
   * @param targer
   */
  public static moveNodeByOtherNode(node: Node, targer: Node, nodePoint = Vec3.ZERO): void {
    let wp: Vec3 = UIUtils.convetOtherNodeSpaceAR(node, targer);
    // let wp = node.getWorldPosition(new Vec3(0, 0, 0));
    node.removeFromParent();
    targer.addChild(node);
    node.setPosition(wp);
    // node.setWorldPosition(wp);
  }
  /**
   * 逆向变换一个空间点，一般用于将世界坐标转换到本地坐标系中。
   * @param node
   * @param targer
   * @returns Vec3
   */
  public static moveNodeByPostion(node: Node, targer: Node): Vec3 {
    let wp = node.getWorldPosition();
    let lp = targer.inverseTransformPoint(new Vec3(targer.getPosition().x, targer.getPosition().y, 0), wp);
    return lp;
  }
  /**
   * 获取某组poker的坐标
   * @param groupIndex
   * @param pokerIndex
   * @returns Vec3
   */
  public static getPokerTargetWorldPostionOfPlay(groupIndex: number, pokerIndex: number): Vec3 {
    let x = groupIndex * GameConfig.PLAY_POKER_X_SPACING;
    let y = pokerIndex * -30;
    return new Vec3(x, y, 0);
  }
  /**
   * 将一个 UI 节点世界坐标系下点转换到另一个 UI 节点 (局部) 空间坐标系，这个坐标系以锚点为原点。
   * @param node
   * @param targetNode
   * @param nodePoint
   * @returns
   */
  public static convetOtherNodeSpaceAR(node: Node, targetNode: Node, nodePoint = Vec3.ZERO): Vec3 {
    const nodeUI = node?.getComponent(UITransform);
    const targetNodeUI = targetNode?.getComponent(UITransform);
    if (nodeUI && targetNodeUI) {
      //先转成世界坐标
      let worldPoint = nodeUI.convertToWorldSpaceAR(nodePoint);
      return targetNodeUI.convertToNodeSpaceAR(worldPoint);
    }
    return nodePoint;
  }
}
